﻿using Esri.ArcGISRuntime.Data;
using Esri.ArcGISRuntime.Geometry;
using Esri.ArcGISRuntime.Mapping;
using Esri.ArcGISRuntime.Symbology;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Color = System.Drawing.Color;

namespace classTest
{
    /// <summary>
    /// UniqueValueRenderWindow.xaml 的交互逻辑
    /// </summary>
    public partial class UniqueValueRenderWindow : Window
    {
        private List<Color> ColorArray = new List<Color> { Color.AliceBlue, Color.AntiqueWhite, Color.Aqua, Color.Aquamarine, Color.Azure, Color.Beige, Color.Bisque, Color.Black, Color.BlanchedAlmond, Color.Blue, Color.Brown, Color.BurlyWood, Color.CadetBlue, Color.Chocolate, Color.Coral, Color.CornflowerBlue, Color.Cornsilk, Color.Crimson, Color.Cyan, Color.FloralWhite, Color.Green, Color.GreenYellow, Color.HotPink, Color.LawnGreen, Color.Magenta };
        private FeatureLayer pLayer;
        private string sField;
        public MainWindow mainWindow { set; get; }
        public UniqueValueRenderWindow(MainWindow mainWindow1)
        {
            InitializeComponent();
            mainWindow = mainWindow1;
            Initialize();
        }

        private void Initialize()
        {
            LayerCollection pLayers = mainWindow.MyMapView.Map.OperationalLayers;
            for (int i = pLayers.Count - 1; i >= 0; i--)
            {
                ComboBoxItem cbi = new ComboBoxItem();
                cbi.Content = pLayers[i].Name;
                cbi.DataContext = pLayers[i];
                LayerCombox.Items.Add(cbi);
            }
        }

        private void Confirm_Click(object sender, RoutedEventArgs e)
        {
            if(pLayer!=null && sField != null)
            {
                SetUniqueRender(pLayer, sField);
                Close();
            }

        }

        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void LayerCombox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (LayerCombox.SelectedIndex != -1)
            {
            FeatureLayer layer = (LayerCombox.SelectedItem as ComboBoxItem).DataContext as FeatureLayer;
            pLayer = layer;
            foreach(Field f in layer.FeatureTable.Fields)
            {
                ComboBoxItem cbi = new ComboBoxItem();
                cbi.Content = f.Name;
                FieldComboBox.Items.Add(cbi);
            }
            FieldComboBox.SelectedIndex = 0;
            }

        }

        private void FieldComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (FieldComboBox.SelectedIndex != -1)
            {
                sField = (FieldComboBox.SelectedItem as ComboBoxItem).Content.ToString();
            }
        }

        // 唯一值作色
        private async void SetUniqueRender(FeatureLayer pLayer, string sFieldName, List<Symbol> pSymbolList = null)
        {
            //创建UniqueValueRenderer()渲染器
            UniqueValueRenderer regionRenderer = new UniqueValueRenderer();
            //设置字段名
            regionRenderer.FieldNames.Add(sFieldName);
            //设置查询条件，默认取出所有记录
            QueryParameters pQueryPara = new QueryParameters();
            FeatureQueryResult queryResult = await pLayer.FeatureTable.QueryFeaturesAsync(pQueryPara);
            //得到查询结果
            object featureValue;
            //List<string> vs = new List<string>();
            int i = 0;
            Random ra = new Random();
            
            //while遍历每一个要素
            foreach (Feature j in queryResult)
            {
                //提取要素中sFieldName对应的字段值
                featureValue = j.GetAttributeValue(sFieldName);
                //for(int k = vs.Count - 1; k >= 0; k--)
                //{
                //    if (vs[k] == featureValue.ToString())
                //    {
                //        return;
                //    }
                //}
                //vs.Add(featureValue.ToString());
                //if判断要素类型，创建对应类型的符号
                Symbol tempSymbol = null;
                if (pSymbolList == null)
                {
                    if (i >= ColorArray.Count)
                        i = 0;
                    Color pColor = ColorArray[i];
                    if (pLayer.FeatureTable.GeometryType == GeometryType.Point || pLayer.FeatureTable.GeometryType == GeometryType.Multipoint)
                    {
                        try
                        {
                            SimpleRenderer ssRenderer = pLayer.Renderer as SimpleRenderer;
                            SimpleMarkerSymbol pointSymbol = ssRenderer.Symbol.Clone() as SimpleMarkerSymbol;
                            tempSymbol = new SimpleMarkerSymbol(pointSymbol.Style, Color.FromArgb(255, ra.Next(0, 255), ra.Next(0, 255), ra.Next(0, 255)), pointSymbol.Size);
                        }
                        catch
                        {
                            tempSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbolStyle.Circle, Color.FromArgb(255, ra.Next(0, 255), ra.Next(0, 255), ra.Next(0, 255)), 5);
                        }
                        
                    }
                    else if (pLayer.FeatureTable.GeometryType == GeometryType.Polyline)
                    {
                        try
                        {
                            SimpleRenderer ssRenderer = pLayer.Renderer as SimpleRenderer;
                            SimpleLineSymbol pointSymbol = ssRenderer.Symbol.Clone() as SimpleLineSymbol;
                            tempSymbol = new SimpleLineSymbol(pointSymbol.Style, Color.FromArgb(255, ra.Next(0, 255), ra.Next(0, 255), ra.Next(0, 255)), pointSymbol.Width);
                    }
                        catch
                    {
                        tempSymbol = new SimpleLineSymbol(SimpleLineSymbolStyle.Solid, Color.FromArgb(255, ra.Next(0, 255), ra.Next(0, 255), ra.Next(0, 255)), 1);
                    }
                }
                    else if (pLayer.FeatureTable.GeometryType == GeometryType.Polygon)
                    {
                        try
                        {
                            SimpleRenderer ssRenderer = pLayer.Renderer as SimpleRenderer;
                            SimpleFillSymbol pointSymbol = ssRenderer.Symbol.Clone() as SimpleFillSymbol;
                            tempSymbol = new SimpleFillSymbol(pointSymbol.Style, Color.FromArgb(255, ra.Next(0, 255), ra.Next(0, 255), ra.Next(0, 255)), pointSymbol.Outline);
                        }
                        catch
                        {
                            SimpleLineSymbol tempSymbol2 = new SimpleLineSymbol(SimpleLineSymbolStyle.Solid, Color.Black, 1);
                            tempSymbol = new SimpleFillSymbol(SimpleFillSymbolStyle.Solid, Color.FromArgb(255, ra.Next(0, 255), ra.Next(0, 255), ra.Next(0, 255)), tempSymbol2);
                        }
                                               
                    }
                }
                else
                {
                    tempSymbol = pSymbolList[i];
                }
                i++;
                //建立符号和字段值的一一对应关系
                UniqueValue pUniqueValue = new UniqueValue("null", "null", tempSymbol, featureValue);
                //将pUniqueValue放入regionRenderer中
                regionRenderer.UniqueValues.Add(pUniqueValue);

            }
            //设置regionRenderer的其它属性
            SimpleFillSymbol defaultFillSymbol = new SimpleFillSymbol(SimpleFillSymbolStyle.Cross, Color.Gray, null);
            regionRenderer.DefaultSymbol = defaultFillSymbol;
            regionRenderer.DefaultLabel = "Other";
            //将regionRenderer赋值给图层，图层即以这种renderer进行显示
            pLayer.Renderer = regionRenderer;
        }
    }
}
